#include <bits/stdc++.h>
#define pi acos(-1)
#define int long long
#define PII pair<int,int>
#define all(v) v.begin(),v.end()
#define INF 0x3f3f3f3f3f3f3f3f
#define fs(a) cout<<fixed<<setprecision(a)<< //fs(4)(1.0/3)=0.3333//保留a位小数
#define read() freopen("input.txt","r",stdin)
#define output() freopen("output.txt","w",stdout)
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e6+10;
const int mod = 1e9+7;
const int Mod = 998244353;
int lowbit(int x){return x&(-x);}
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}//       a/b向上取整
int quickpow(int a,int n){int ans=1;while(n){if(n&1){ans*=a,ans%=Mod;}a*=a;a%=Mod;n>>=1;}return ans;}//快速幂
int qc(int a,int b,int p){int ans=0;while(b){if(b&1){ans+=a,ans%=p;}a*=2;a%=p;b>>=1;}return ans;}//快速乘 a*b%p
int a[N],b[N],sum[N],mp[51][51];
 
inline void solve() {
	int n;cin>>n;
	int l=1,r=n*n;
	int cnt=0;
	for(int i=1;i<=n;i++){//行
		if(i&1){//奇数行
			for(int j=1;j<=n;j++,cnt++){
				if(cnt&1) mp[i][j]=r--;
				else mp[i][j]=l++;
			}
		}
		else{//偶数行
			for(int j=n;j>=1;j--,cnt++){
				if(cnt&1) mp[i][j]=r--;
				else mp[i][j]=l++;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)
			cout<<mp[i][j]<<" ";
	    cout<<"\n";
	}
}
signed main(){
	fast;int T;cin>>T;
	while(T--) solve();
}